package com.gopay.common.cps.dao.aggregatepayment.impl;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.cps.dao.aggregatepayment.ApContractInfoQueryDAO;
import com.gopay.common.domain.aggregatepayment.ApContractInfo;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Chenyu Li
 * @description 聚合支付合同查询DAO
 * @date 2018/1/2
 * Copyright (c) 2017. www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("apContractInfoQueryDAO")
public class ApContractInfoQueryDAOImpl extends BaseDAOImpl<ApContractInfo, String> implements ApContractInfoQueryDAO {

    /**
     * 根据custId查找合同信息
     * @author Chenyu Li
     * @date 2018/1/2
     * @params [custId]
     * @return java.util.List<com.gopay.common.domain.aggregatepayment.ApContractInfo>
     * @throws
     */
    @Override
    public List<ApContractInfo> getInfoByCustId(String custId,int pageNo , int pageSize){
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ApContractInfo.class);
        detachedCriteria.add(Restrictions.eq("custId", custId));
        return findByCriteria(detachedCriteria,pageNo,pageSize);
    }

    /**
     * 根据code查找合同信息
     * @author Chenyu Li
     * @date 2018/1/2
     * @params [code]
     * @return com.gopay.common.domain.aggregatepayment.ApContractInfo
     * @throws
     */
    @Override
    public List<ApContractInfo> getInfoByCode(String code){
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ApContractInfo.class);
        detachedCriteria.add(Restrictions.eq("code", code));
        return findByCriteria(detachedCriteria);
    }

    /**
     * 根据code查找合同信息数量
     * @author Chenyu Li
     * @date 2018/1/11
     * @params [custId]
     * @return int
     * @throws
     */
    @Override
    public int getInfoCountByCustId(String custId){

        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ApContractInfo.class);
        detachedCriteria.add(Restrictions.eq("custId", custId));
        return findCountByCriteria(detachedCriteria);
    }

    /**
     * 根据合同审核状态和合同到期时间查询合同
     * @author Chenyu Li
     * @date 2018/1/11
     * @params [autoSign, status, endDate]
     * @return java.util.List<com.gopay.common.domain.aggregatepayment.ApContractInfo>
     * @throws
     */
    @Override
    public List<ApContractInfo> getInfoByStatusAndEndDate(String autoSign, String status, String endDate) {

        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ApContractInfo.class);
        detachedCriteria.add(Restrictions.eq("autoSign", autoSign));
        detachedCriteria.add(Restrictions.eq("status", status));
        detachedCriteria.add(Restrictions.le("edate", endDate));
        return findByCriteria(detachedCriteria);
    }

    @Override
    public int getApContractInfoCount(ApContractInfo apContractInfo) {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ApContractInfo.class);
        if(StringUtils.isNotBlank(apContractInfo.getCustId())){
            detachedCriteria.add(Restrictions.eq("custId", apContractInfo.getCustId()));
        }

        if(StringUtils.isNotBlank(apContractInfo.getStatus())) {
            detachedCriteria.add(Restrictions.eq("status", apContractInfo.getStatus()));
        }
        return findCountByCriteria(detachedCriteria);
    }

    @Override
    public ApContractInfo getApContractInfo(ApContractInfo apContractInfo) {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(ApContractInfo.class);
        if(StringUtils.isNotBlank(apContractInfo.getCustId())){
            detachedCriteria.add(Restrictions.eq("custId", apContractInfo.getCustId()));
        }

        if(StringUtils.isNotBlank(apContractInfo.getStatus())) {
            detachedCriteria.add(Restrictions.eq("status", apContractInfo.getStatus()));
        }

        if(StringUtils.isNotBlank(apContractInfo.getType())) {
            detachedCriteria.add(Restrictions.eq("type", apContractInfo.getType()));
        }

        if(StringUtils.isNotBlank(apContractInfo.getCttId())) {
            detachedCriteria.add(Restrictions.eq("cttId", apContractInfo.getCttId()));
        }
        if(StringUtils.isNotBlank(apContractInfo.getEdate())) {
            detachedCriteria.add(Restrictions.le("edate", apContractInfo.getEdate()));
        }
        if(StringUtils.isNotBlank(apContractInfo.getAutoSign())) {
            detachedCriteria.add(Restrictions.eq("autoSign", apContractInfo.getAutoSign()));
        }
        List<ApContractInfo> list = findByCriteria(detachedCriteria);

        if(list != null && list.size() > 0) {
            return list.get(0);
        }

        return null;
    }
}
